package org.dlsu.mt.parser.generator;


/**
 * This class represents an entry inside the parse table.
 * An entry contains an action(shift,reduce,goto,accept,error),a state, and a rule
 * When the action is a shift, the state will have an entry but the rule will be null
 * When the action is a reduce, the state will be -1 and the rule will contain the reduction rule to use
 * When the action is accept or error, the state will be -1 and the rule null
 */
public class ParseTableEntry {
	protected String action;
	protected int state;
	protected Rule rule;

	/**
	 * Creates a parse table entry with an action, a goto state, and reduction rule (if there is one)
	 *
	 * @param action The corresponding action of the parse table
	 * @param state The corresponding goto state
	 * @param rule The corresponding reduction rule
	 */
	public ParseTableEntry(String action, int state, Rule rule) {
		this.action = action;
		this.state = state;
		this.rule = rule;
	}

	/**
	 * Gets the corresponding action of the entry
	 *
	 * @return The action of the entry as a string
	 */
	public String getAction(){
		return action;
	}

	/**
	 * Gets the corresponding goto state
	 *
	 * @return The next state for the shift and goto actions
	 */
	public int getState(){
		return state;
	}

	/**
	 * Gets the corresponding reduction rule of the entry
	 *
	 * @return The reduction rule for this parse table entry
	 */
	public Rule getRule() {
		return rule;
	}

	/**
	 * Gets the corresponding Left-hand side of the reduction rule/production
	 *
	 * @return The left-hand side of the reduction rule in the parse table entry
	 */
	public String getRuleLHS() {
		return rule.getLHS();
	}

	/**
	 * Gets the corresponding size of the Right-hand side of the reduction rule/production
	 *
	 * @return The size of the rule
	 */
	public int getRuleSize(){
		if (rule.rhs.elementAt(0).equals(ParseTable.EMPTY))
			return 0;
		return rule.getRHSTermSize();
	}

	/**
	 * Returns the string representation of the table entry as one of the following:
	 * Reduce action:
	 *		reduce:<rule string>
	 * Shift action:
	 *		shift:<state>
	 * Error action:
	 *		error
	 * Accept action:
	 *		accept
	 */
	public String toString() {
		if (rule != null)
			return action + ":"+rule.toString();
		else if (state != -1)
			return action + ":"+state;
		else
			return action;
	}
}